package com.vecspace.statistics.counter;

/**码表，用于记录开始执行时间和结束执行时间
 *  
 * @version: 1.0
 * @author: wanghua
 */
public class Stopwatch implements Comparable<Stopwatch>{
	
	public Stopwatch() {
		setStartTime(System.currentTimeMillis());
	}
	
	public Stopwatch(String name) {
		this();
		setName(name);
	}
	
	private String name;
	//开始时间
	private long startTime;
	//结束时间
	private long endTime;
	
	public long getStartTime() {
		return startTime;
	}
	public void setStartTime(long startTime) {
		this.startTime = startTime;
		this.endTime = startTime;
	}
	public long getEndTime() {
		return endTime;
	}
	public void setEndTime(long endTime) {
		this.endTime = endTime;
	}
	/**开始计时
	 * @author: wanghua
	 */
	public void startTicking() {
		setStartTime(System.currentTimeMillis());
	}
	
	/**结束计时
	 * @author: wanghua
	 */
	public void stopTicking() {
		setEndTime(System.currentTimeMillis());
	}
	
	/**返回开始到结束之间的秒数
	 * @return 
	 * @author: wanghua
	 */
	public double getElapsedSeconds() {
		long millis = endTime - startTime;
		return millis/ 1000.0;
	}
	
	/**返回开始到结束之间的毫秒数
	 * @return 
	 * @author: wanghua
	 */
	public long getElapsedMillisSeconds() {
		long millis = endTime - startTime;
		return millis;
	}
	
	public void reset() {
		startTime = endTime = 0;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * 用开始时间进行对比
	 */
	@Override
	public int compareTo(Stopwatch o) {
		if(o == null)return 1;
		if(startTime == o.startTime)return 0;
		else if(startTime > o.startTime) return 1;
		else return -1;
	}
	
}
